//
//  MaterialViewController.m
//  Article
//
//  Created by LONG on 16/4/21.
//  Copyright © 2016年 mac. All rights reserved.
//

#import "MaterialViewController.h"
#import "MaterialTitleView.h"

#import "OllViewController.h"
#import "FirstViewController.h"
#import "SecondViewController.h"
#import "ThirdViewController.h"

@interface MaterialViewController ()<MaterialTitleViewDelegate,UIScrollViewDelegate>
//头部的选项卡
@property(nonatomic,strong) MaterialTitleView *titleView;

//滚动条
@property(nonatomic,strong) UIScrollView *scrollView;

//大数组，子控制器的
@property(nonatomic,strong) NSMutableArray *childViews;
@end

@implementation MaterialViewController
- (NSMutableArray *)childViews
{
    if(_childViews==nil)
    {
        _childViews =[NSMutableArray array];
    }
    return _childViews;
}

- (UIScrollView *)scrollView
{
    if(_scrollView==nil)
    {
        
        _scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, _titleView.frame.size.height, WIDTH, HEIGHT - _titleView.frame.size.height)];
        _scrollView.delegate=self;
        _scrollView.showsHorizontalScrollIndicator = NO;
        _scrollView.showsVerticalScrollIndicator = NO;
        _scrollView.pagingEnabled = YES;
        _scrollView.contentSize = CGSizeMake(4 * WIDTH, 0);
        _scrollView.bounces = NO;
        [self.view addSubview:_scrollView];
    }
    return _scrollView;
}


- (MaterialTitleView *)titleView
{
    if(_titleView==nil)
    {
        _titleView =[MaterialTitleView new];
        _titleView.layer.borderColor = [UIColorFromRGB(0xf2f2f2) CGColor];
        _titleView.layer.borderWidth = 1;
        _titleView.delegate=self;
        _titleView.frame = CGRectMake(0, 0, self.view.frame.size.width, 35);
        [self.view addSubview:_titleView];
        
    }
    return _titleView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"我的团队";
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"arrow"] style:UIBarButtonItemStylePlain target:self action:@selector(back)];
    self.navigationItem.leftBarButtonItem.tintColor = [UIColor whiteColor];
    // Do any additional setup after loading the view.
    self.view.backgroundColor =  [UIColor whiteColor];
    
    //添加头部的view
    [self titleView];
    
    //添加scrollView
    [self scrollView];
    
    //添加各子控制器
    [self setupChildVcs];
    
}
-(void)back{
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark 添加各子控制器
- (void)setupChildVcs
{
    //全部控制器
    OllViewController *OVc =[OllViewController new];
    [self addChildViewController:OVc];
    [self.childViews addObject:OVc.view];
    
    //一级控制器
    FirstViewController *TVc =[FirstViewController new];
    
    [self addChildViewController:TVc];
    [self.childViews addObject:TVc.view];
    
    //二级控制器
    SecondViewController *TwVc =[SecondViewController new];
    [self addChildViewController:TwVc];
    [self.childViews addObject:TwVc.view];
    
    //三级控制器
    ThirdViewController *MVc =[ThirdViewController new];
    [self addChildViewController:MVc];
    [self.childViews addObject:MVc.view];

    
    
    for(int i=0;i<self.childViews.count;i++)
    {
        UIView *childV = self.childViews[i];
        CGFloat childVX = WIDTH * i ;
        childV.frame = CGRectMake(childVX, 0, WIDTH, self.view.frame.size.height - _titleView.frame.size.height);
        [_scrollView addSubview:childV];
        
    }
    
    
}


#pragma mark 滚动条
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    
    [self.view endEditing:YES];
    
    
    if(scrollView==_scrollView)
    {
        if(_scrollView.contentOffset.x / WIDTH ==0)
        {
            [_titleView wanerSelected:0];
        }
        else if (_scrollView.contentOffset.x / WIDTH ==1)
        {
            [_titleView wanerSelected:1];
        }else if (_scrollView.contentOffset.x / WIDTH ==2)
        {
            [_titleView wanerSelected:2];
        }
        else if (_scrollView.contentOffset.x / WIDTH ==3)
        {
            [_titleView wanerSelected:3];
        }
        
    }
}

#pragma mark titleView的方法
- (void)titleView:(MaterialTitleView *)titleView scollToIndex:(NSInteger)tagIndex
{
    
    
    [_scrollView setContentOffset:CGPointMake(tagIndex * WIDTH, 0) animated:YES];
    
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    self.tabBarController.tabBar.hidden = YES;
    self.tabBarController.tabBar.translucent = YES;
}
@end
